package com.devstringx.pmt.service.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.devstringx.pmt.domain.common.UserDetailsDTO;
import com.devstringx.pmt.persitence.common.CommonApplicationDAO;

@Service
@Transactional(propagation=Propagation.REQUIRED)
public class UserManagmentService {

	@Autowired
	CommonApplicationDAO commonApplicationDAO;
	
	public void changePassword(String oldPassword, String newPassword) {
		
		UserDetailsDTO userDetails = (UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder(256);
		String encPassword = bCryptPasswordEncoder.encode(userDetails.getPassword());
		userDetails.setPassword(encPassword);
	    commonApplicationDAO.updateExistingRow(userDetails);
		//userDetails.setAuthorities(userDetails.getAdditionalAuthorities());
		/*AuthenticationDAO authenticationDAO = getAuthenticationDAO();
		authenticationDAO.updateUser(userDetails);*/
		
	}

	public void createUser(UserDetailsDTO userDetails) {
		commonApplicationDAO.insertNewRow(userDetails);
		
	}

	public void deleteUser(String arg0) {
	
		
	}

	public void updateUser(UserDetails arg0) {
	
		
	}

	/*public boolean userExists(String userId) {
		boolean status=false; 
		
	  UserDetailsDTO detailsDTO= commonApplicationDAO.getDetailsForUserId(userId);
		if(null!=detailsDTO){
			status=true;
		}
		
		
		return status;
	}
	
	public void accountLocked(UserDetailsDTO detailsDTO){
		
		int count = detailsDTO.getFailureCount();
		count ++ ;
		detailsDTO.setFailureCount(count);
		
		if(count >= 5){
			detailsDTO.setAccountNonLocked(true);
			detailsDTO.setFailureCount(0);
		}
		AuthenticationDAO authenticationDAO = getAuthenticationDAO();
		authenticationDAO.updateUser(detailsDTO);
	}
	
*/
}
